{"values":{"name":"Atnaujinti klientų informacija iš Excel failo","code":"C-001","description":null,"triggerTypeId":0,"placeId":"clients-list","scriptContent":"\n//await log.info(\"Importing file: \"+initial.fileName);\n\nconst PRICE_CATEGORIES = await rql(`@ALL SELECT id, code, name FROM priceCategories WHERE active = true`);\nconst PAYMENT_TERMS    = await rql(`@ALL SELECT id, code, name FROM paymentTerms WHERE active = true`);\n\nconst fileDataResponse = await fetchData(undefined, { url: initial.fileName, method: \"GET\", raw: false });\n\nconst clientsFileData = await dataParser.excelSheetToJson(fileDataResponse.body, 0, 5);\n//await log.debug(clientsFileData.slice(0, 10000));\nawait processFileData(clientsFileData, processClientFileRow);\n\n//const bankAccountsFileData = await dataParser.excelSheetToJson(fileDataResponse.body, 2, 2);\n//await log.debug(bankAccountsFileData.slice(0, 10000));\n//await processFileData(bankAccountsFileData, processBankAccountFileRow);\n\n//await log.trace('PRICE_CATEGORIES: '+JSON.stringify(PRICE_CATEGORIES, null, 2));\n//await log.trace('PAYMENT_TERMS: '+JSON.stringify(PAYMENT_TERMS, null, 2));\n\noutput = {message: \"Veiksmas atliktas\"}\n\n\n\n/************************************/\n/************************************/\n\nasync function processFileData(fileData, refToFunction) {\n    const maxConcurrency = 10; // Maksimalus vienu metu vykdomų užduočių skaičius\n    const results = [];\n    const executing = new Set();\n\n    for (const rowData of fileData) {\n        const task = refToFunction(rowData);\n        results.push(task);\n        executing.add(task);\n\n        // Kai užduotis baigiasi, pašaliname ją iš \"vykdomų\"\n        task.finally(() => executing.delete(task));\n\n        // Jei pasiektas limitas, laukiame, kol bent viena užduotis baigsis\n        if (executing.size >= maxConcurrency) {\n            await Promise.race(executing);\n        }\n    }\n\n    await Promise.all(executing);\n    return Promise.all(results);\n}\n\n\nasync function processClientFileRow(fileRowData) {\n\t//await log.info(JSON.stringify(fileRowData, null, 2));\n\ttry {\n\t\tconst clientCode = fileRowData.Code;\n\t\tif (clientCode == null) return;\n\t\tconst client = await getClient(clientCode);\n\t\tif (client == null) {\n\t\t\t//await createClient(fileRowData);\n\t\t} else {\n\t\t\tawait updateClient(client, fileRowData);\n\t\t}\n\t} catch (execption) {\n        await logException(`processClientFileRow ${fileRowData.Code} exception: `, exception);\n\t}\n}\n\nasync function processBankAccountFileRow(fileRowData) {\n\t//await log.info(JSON.stringify(fileRowData, null, 2));\n\ttry {\n\t\tconst clientCode = fileRowData[\"##refId##\"];\n\t\tif (clientCode == null) return;\n\t\tconst client = await getClient(clientCode);\n\t\tif (client == null) return;\n\t\tawait createBankAccount(client.id, fileRowData);\n\t} catch (exception) {\n        await logException(`processBankAccountFileRow ${fileRowData[\"##refId##\"]} exception: `, exception);\n\t}\n}\n\nasync function getClient(clientCode) {\n\t//await log.debug(`getClient ${clientCode}`);\n\ttry {\n\t\tconst clients = await rql(`SELECT id, importReference, name, code, typeId, taxCodeTypeId, regCode, vatNumber, address, email, phone, isafSpecTax, dateOfBirth, isCustomer, isSupplier, autoReconciliationTypeId, accountingCategory.id, priceCategory.id, customerPaymentTerm.id, supplierPaymentTerm.id, supplierTaxCategory.id, customerTaxCategory.id FROM clients WHERE code = '${clientCode}'`);\n\t\tif ((clients.content[0] == null) || (clients.content[0] == undefined)) return null;\n\t\treturn clients.content[0];\n\t} catch (exception) {\n        await logException(`getClient ${clientCode} exception: `, exception);\n\t\treturn null;\n\t}\n}\n\nasync function createClient(fileRowData) {\n\tawait log.trace(`createClient ${fileRowData.Code}`);\n\ttry {\n\t\tconst clientInput = await makeClientInput(fileRowData);\n\t\tconst createClientReq = { client: clientInput, clientGroups: null, companyId: null };\n\t\t//await mutate(`createClient`, createClientReq);\n\t}\n\tcatch (exception) {\n        await logException(`createClient ${fileRowData.Code} exception: `, exception);\n\t}\n}\n\nasync function updateClient(client, fileRowData) {\n\tawait log.trace(`updateClient ${fileRowData.Code}`);\n\ttry {\n\t\tconst clientInput = await mergeToClientInput(fileRowData, client);\n\t\tconst updateClientReq = { clientId: client.id, client: clientInput, clientGroups: null, companyId: null };\n\t\tawait mutate(`updateClient`, updateClientReq);\n\t}\n\tcatch (exception) {\n        await logException(`updateClient ${fileRowData.Code} exception: `, exception);\n\t}\n}\n\nasync function createBankAccount(clientId, fileRowData) {\n\tawait log.trace(`createBankAccount ${fileRowData.Name}`);\n    try {\n\t\tconst bankAccount = makeBankAccount(clientId, fileRowData.Name, fileRowData.AccountNumber, fileRowData.BankCode, fileRowData.IsMain);\n        await mutate(`createBankAccount`, { bankAccount, clientId });\n    } catch (exception) {\n        await logException(`createBankAccount ${fileRowData.Name} exception: `, exception);\n    }\n}\n\nasync function updateBankAccount(clientId, fileRowData) {\n\tawait log.trace(`updateBankAccount ${fileRowData.Name}`);\n    try {\n\t\tconst bankAccount = makeBankAccount(clientId, fileRowData.Name, fileRowData.AccountNumber, fileRowData.BankCode, fileRowData.IsMain);\n        await mutate(`updateBankAccount`, { bankAccount, clientId });\n    } catch (exception) {\n        await logException(`updateBankAccount ${fileRowData.Name} exception: `, exception);\n    }\n}\n\n\nasync function createAddress(clientId, address) {\n\tawait log.trace(`createAddress ${clientId}`);\n    try {\n    \taddress.name = \"Sąskaitos adresas\";\n        await mutate(`createAddress`, { address, clientId });\n    } catch (exception) {\n        await logException(`createAddress exception: `, exception);\n    }\n}\n\nfunction makeBankAccount(clientId, accountName, accountNumber, bankCode, isMain) {\n\tconst bankAccount =\t{\n\t\t\"clientId\": clientId,\n\t\t\"name\": accountName,\n\t\t\"accountNumber\": accountNumber,\n\t\t\"bankId\": null,\n\t\t\"isMain\": Boolean(isMain),\n\t\t\"active\": true,\n\t\t\"activeFrom\": null,\n\t\t\"activeTo\": null\n\t};\n\treturn bankAccount;\n}\n\n\n\nasync function makeClientInput(fileRowData) {\n\tconst clientInput = {};\n\tclientInput.name          = fileRowData.Name;\n\tclientInput.code          = fileRowData.Code;\n\tclientInput.typeId        = fileRowData.TypeId;\n\tclientInput.taxCodeTypeId = fileRowData.TaxCodeTypeId;\n\tclientInput.regCode       = fileRowData.RegCode;\n\tclientInput.vatNumber     = fileRowData.VatNumber;\n\tclientInput.address       = fileRowData.Address;\n\tclientInput.email         = fileRowData.Email;\n\tclientInput.phone         = fileRowData.Phone;\n\tclientInput.isafSpecTax   = Boolean(fileRowData.IsafSpecTax);\n\tclientInput.dateOfBirth   = fileRowData.DateOfBirth;\n\tclientInput.isCustomer    = Boolean(fileRowData.IsCustomer);\n\tclientInput.isSupplier    = Boolean(fileRowData.IsSupplier);\n\n\tclientInput.autoReconciliationTypeId = fileRowData.AutoReconciliationTypeId;\n\n\t//clientInput.importReference       = fileRowData.refId;\n\t//clientInput.priceCategoryId       = null;\n\t//clientInput.customerPaymentTermId = null;\n\t//clientInput.companyId             = null;\n\t//clientInput.accountingCategoryId  = null;\n\t//clientInput.supplierPaymentTermId = null;\n\t//clientInput.supplierTaxCategoryId = nul;\n\n\t//await log.trace('makeClientInput: '+JSON.stringify(clientInput, null, 2));\n\treturn clientInput;\n}\n\nasync function mergeToClientInput(fileRowData, client) {\n\tconst clientInput = {}\n\n\tclientInput.name          = nvl(fileRowData.Name,          client.name);\n\tclientInput.code          = nvl(fileRowData.Code,          client.code);\n\tclientInput.typeId        = nvl(fileRowData.TypeId,        client.typeId);\n\tclientInput.taxCodeTypeId = nvl(fileRowData.TaxCodeTypeId, client.taxCodeTypeId);\n\tclientInput.regCode       = nvl(fileRowData.RegCode,       client.regCode);\n\tclientInput.vatNumber     = nvl(fileRowData.VatNumber,     client.vatNumber);\n\tclientInput.address       = nvl(fileRowData.Address,       client.address);\n\tclientInput.email         = nvl(fileRowData.Email,         client.email);\n\tclientInput.phone         = nvl(fileRowData.Phone,         client.phone);\n\tclientInput.isafSpecTax   = nvl(Boolean(fileRowData.IsafSpecTax), client.isafSpecTax);\n\tclientInput.dateOfBirth   = nvl(fileRowData.DateOfBirth,          client.dateOfBirth);\n\tclientInput.isCustomer    = nvl(Boolean(fileRowData.IsCustomer),  client.isCustomer);\n\tclientInput.isSupplier    = nvl(Boolean(fileRowData.IsSupplier),  client.isSupplier);\n\n\tclientInput.autoReconciliationTypeId = nvl(fileRowData.AutoReconciliationTypeId, client.autoReconciliationTypeId);\n\n\tconst priceCategoryId       = await getIdByCodeFromList(PRICE_CATEGORIES, fileRowData.PriceCategoryCode,       client.priceCategory?.id);\n\tconst customerPaymentTermId = await getIdByCodeFromList(PAYMENT_TERMS,    fileRowData.CustomerPaymentTermCode, client.customerPaymentTerm?.id);\n\tconst supplierPaymentTermId = await getIdByCodeFromList(PAYMENT_TERMS,    fileRowData.SupplierPaymentTermCode, client.supplierPaymentTerm?.id);\n\n\tclientInput.companyId             = client.companyId;\n\tclientInput.importReference       = nvl(String(fileRowData[\"##refId##\"]), client.importReference);\n\tclientInput.priceCategoryId       = priceCategoryId;\n\n\tclientInput.accountingCategoryId  = client.accountingCategory?.id;\n\tclientInput.customerTaxCategoryId = client.customerTaxCategory?.id;\n\tclientInput.customerPaymentTermId = customerPaymentTermId;\n\tclientInput.supplierTaxCategoryId = client.supplierTaxCategory?.id;\n\tclientInput.supplierPaymentTermId = supplierPaymentTermId;\n\n\t//await log.trace('mergeToClientInput: '+JSON.stringify(clientInput, null, 2));\n\treturn clientInput;\n}\n\n\nasync function getIdByCodeFromList(list, code, defaultId) {\n\ttry {\n\t\t//await log.trace(`getIdByCodeFromList code: ${code} defaultId: ${defaultId} list: `+JSON.stringify(list, null, 2));\n\t\tif (code == null) return defaultId;\n\t\tif (list == null) return defaultId;\n\t\tconst id = list.find(f => f.code == code)?.id;\n\t\tif (id == null) return defaultId;\n\t\treturn id;\n\t} catch (exception) {\n        await logException(`getIdByCodeFromList ${code} exception: `, exception);\n\t\treturn defaultId;\n\t}\n}\n\nfunction nvl(valueA, valueB) {\n\tif (valueA !== null && valueA !== undefined) return valueA;\n\treturn valueB;\n}\n\nasync function logException(tag, exception) {\n\tconst serializedError = {\n\t\tmessage: exception.message,\n\t\tstack: exception.stack,\n\t\tname: exception.name,\n\t};\n\tawait log.error(tag + JSON.stringify(serializedError, null, 2));\n}","paramsFormEnabled":true,"paramsFormSchema":"[\n\t{\n\t\t\"blockType\": \"FILEUPLOAD\",\n\t\t\"meta\": {\n\t\t\t\"fieldName\": \"fileName\",\n\t\t\t\"accept\": \".xlsx\"\n\t\t}\n\t}\n]","exampleData":"{\n\t\"id\": \"ba8b1704-a27e-4de3-acde-92b4407da4d1\"\n}","activeFrom":null,"activeTo":null,"active":true,"appDefinitionId":"ed57460c-6450-45b2-805f-4a9a072aef61"},"additionalPlaces":[]}